package com.android.sha.ui;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Typeface;
import android.view.MotionEvent;

import com.android.angle.AngleActivity;
import com.android.angle.AngleFont;
import com.android.angle.AngleObject;
import com.android.angle.AngleString;
import com.android.angle.AngleUI;
import com.android.sha.activity.R;
import com.android.sha.common.MyCommon;
import com.android.sha.sprite.BloodSprite;
import com.android.sha.sprite.CardSprite;
import com.android.sha.sprite.PlayerSprite;
import com.android.sha.tools.SpriteHelper;

public class GameUI extends AngleUI{

	private List<PlayerSprite> players;
	private PlayerSprite mPlayer;
	private PlayerSprite oPlayer;
	private List<BloodSprite> mBlood =new ArrayList<BloodSprite>();
	private List<BloodSprite> oBlood = new ArrayList<BloodSprite>();
	private List<CardSprite> mcards = new ArrayList<CardSprite>();
	private List<CardSprite> ocards = new ArrayList<CardSprite>();
	private AngleFont gameFont = new AngleFont(mActivity.mGLSurfaceView, 25, Typeface.createFromAsset(mActivity.getAssets(),"cafe.ttf"), 222, 0, 0, 30, 200, 155, 255);
	private AngleString[] mAblity;
	private AngleString[] oAblity;
	private AngleObject sprites;
	private SpriteHelper spriteHelper;
	public GameUI(AngleActivity activity) {
		super(activity);
		spriteHelper = new SpriteHelper(activity);
		init();
	}
	
	
	public void init(){
		sprites = new AngleObject(200);
		initPlayers();//初始化人物牌
		initCards();//初始化卡牌
		initCardsPosition();//初始化卡牌位置
		initBlood(4,3);//初始化血牌
		initAbility();
		mActivity.mGLSurfaceView.addObject(sprites);//将所有精灵加入主surfaceView
	}
	
	public void initPlayers(){
		players = new ArrayList<PlayerSprite>();
		mPlayer = new PlayerSprite(spriteHelper.getPlayerLayout(R.drawable.guanyu));
		mPlayer.mPosition.set(MyCommon.PLAYERWIDTH/2,MyCommon.HEIGHT - MyCommon.PLAYERHEIGHT/2);
		
		oPlayer = new PlayerSprite(spriteHelper.getPlayerLayout(R.drawable.zhugeliang));
		oPlayer.mPosition.set(MyCommon.PLAYERWIDTH/2,MyCommon.PLAYERHEIGHT/2);
		
		players.add(mPlayer);
		players.add(oPlayer);
		for(int i=0;i<players.size();i++){
			sprites.addObject(players.get(i));
		}
		
	}
	
	public void initCards(){
		for(int i=0;i<4;i++){
			CardSprite card =new CardSprite(spriteHelper.getCardLayout(R.drawable.cardsha));
			mcards.add(card);
		}
		
		
		for(int i=0;i<4;i++){
			CardSprite card =new CardSprite(spriteHelper.getCardLayout(R.drawable.cardback));
			ocards.add(card);
		}
		for(int i=0;i<mcards.size();i++){
			sprites.addObject(mcards.get(i));
		}
		for(int i=0;i<ocards.size();i++){
			sprites.addObject(ocards.get(i));
		}
	}
	public void  initBlood(int mNum,int oNum){
		for(int i=0;i<mNum;i++){
			BloodSprite blood = new BloodSprite(spriteHelper.getBloodLayout(R.drawable.redblood));
			blood.mPosition.set(MyCommon.BLOODWIDTH/2+i*MyCommon.BLOODWIDTH,MyCommon.HEIGHT - MyCommon.PLAYERHEIGHT-MyCommon.BLOODHEIGHT/2);
			sprites.addObject(blood);
			mBlood.add(blood);
		}
		for(int i=0;i<oNum;i++){
			BloodSprite blood = new BloodSprite(spriteHelper.getBloodLayout(R.drawable.redblood));
			blood.mPosition.set(MyCommon.BLOODWIDTH/2+i*MyCommon.BLOODWIDTH,MyCommon.PLAYERHEIGHT+MyCommon.BLOODHEIGHT/2);
			sprites.addObject(blood);
			oBlood.add(blood);
		}
	}
	
	public void initCardsPosition(){
		for(int i=0;i<mcards.size();i++){
			CardSprite card = mcards.get(i);
			card.mPosition.set(MyCommon.PLAYERWIDTH+MyCommon.CARDWIDTH/2+i*MyCommon.CARDWIDTH,  MyCommon.HEIGHT -  MyCommon.CARDHEIGHT/2);
		}
		for(int i=0;i<ocards.size();i++){
			CardSprite card = ocards.get(i);
			card.mPosition.set(MyCommon.PLAYERWIDTH+MyCommon.CARDWIDTH/2+i*MyCommon.CARDWIDTH,MyCommon.CARDHEIGHT/2);
		}
	}
	
	public void initAbility(){
		mAblity = new AngleString[2];
		mAblity[0] = new AngleString(gameFont,"xxx",0,0,AngleString.aCenter);
		mAblity[0].mPosition.set(220,MyCommon.PLAYERHEIGHT+MyCommon.BLOODHEIGHT+20);
		mAblity[1] = new AngleString(gameFont,"我的",0,0, AngleString.aCenter);
		mAblity[1].mPosition.set(120,MyCommon.PLAYERHEIGHT+MyCommon.BLOODHEIGHT+20);
		mActivity.mGLSurfaceView.addObject(mAblity[0]);
		mActivity.mGLSurfaceView.addObject(mAblity[1]);
	}
	
	public void initAbilityPosition(){
		
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(!MyCommon.isCardOuting){
			if (event.getAction() == MotionEvent.ACTION_DOWN){
				float eX = event.getX();
				float eY = event.getY();
				for(int i=0;i<mcards.size();i++){
					CardSprite card = mcards.get(i);
					if(spriteHelper.test(card,eX,eY,MyCommon.CARDWIDTH,MyCommon.CARDHEIGHT)){
						if(card.mPosition.mY == MyCommon.HEIGHT -  MyCommon.CARDHEIGHT/2){
							initCardsPosition();
							card.mPosition.mY =card.mPosition.mY-20;
						}else{
							MyCommon.isCardOuting = true;
							card.outCard = true;
							mcards.remove(i);
							initCardsPosition();
						}
						
					}
				}
			}
		}
		return super.onTouchEvent(event);
	}

}
